#include "daemon_process.h"

static  int  is_running = 1;
static  daemon_sub_process_t *sub_process[SUB_PROCESS_NUM] ;


static void exit_hanlder(int signo)
{
    is_running = 0;
}

/**
 * @brief 运行守护进程
 */

 void daemon_process_run(void)
 {
    // 1 初始化 
      signal(SIGTERM, exit_hanlder);
      signal(SIGINT, exit_hanlder);

      sub_process[0] = daemon_sub_process_init("app");
      sub_process[1] = daemon_sub_process_init("ota");

    // 2 开始周期性的检查 
    while(is_running)
    {
        
        for (size_t i = 0; i < SUB_PROCESS_NUM; i++)
        {
             daemon_sub_process_check_and_start(sub_process[i]);
        }
         
        sleep(1);
    }
    // 3  结束销毁

    for(size_t i = 0; i < SUB_PROCESS_NUM; i++)
    {
        daemon_sub_process_destroy(sub_process[i]);

    }
    
 }